package com.honeycomb.action.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import com.honeycomb.action.BaseAjaxAction;
import com.honeycomb.service.IArticleService;
import com.honeycomb.service.ITagService;
import com.honeycomb.service.IUserService;
import com.honeycomb.utils.ErrorMsgConstants;
import com.honeycomb.utils.HoneyCombColumnIndicator;
import com.honeycomb.utils.HoneyCombTools;
import com.honeycomb.utils.SystemConstants;
import com.honeycomb.vo.ArticleinfoVO;
import com.honeycomb.vo.BaseVO;
import com.honeycomb.vo.MapInfoVO;
import com.honeycomb.vo.ResultDto;
import com.honeycomb.vo.TaginfoVO;
import com.honeycomb.vo.UserInfoVO;


/**
 * 
 * @author ZhongJiang
 * 
 * @function AJAX请求添加article
 * 
 * @param articleTitle 文章标题
 * 
 * @param articleContent 文章内容
 * 
 * @param articleTags 文章标签
 * 
 * @param mapInfo 文章地理信息
 * 
 * @return json
 * 
 */
public class AjaxAddArticleAction extends BaseAjaxAction {
	
	private IArticleService iArticleService;
	private IUserService iUserService;
	private ITagService iTagService;
	
	private String articleTitle;
	private String articleContent;
	private String articleSummary;
	private String tagValue;
	private String mapX;
	private String mapY;
	private String mapAddr;
	private String mapDesc;
	private String mapProvider;
	private String mapType;
	
	public IArticleService getiArticleService() {
		return iArticleService;
	}
	public void setiArticleService(IArticleService iArticleService) {
		this.iArticleService = iArticleService;
	}
	public IUserService getiUserService() {
		return iUserService;
	}
	public void setiUserService(IUserService iUserService) {
		this.iUserService = iUserService;
	}
	public ITagService getiTagService() {
		return iTagService;
	}
	public void setiTagService(ITagService iTagService) {
		this.iTagService = iTagService;
	}
	public String getArticleTitle() {
		return articleTitle;
	}
	public void setArticleTitle(String articleTitle) {
		this.articleTitle = articleTitle;
	}
	public String getArticleContent() {
		return articleContent;
	}
	public void setArticleContent(String articleContent) {
		this.articleContent = articleContent;
	}
	public String getArticleSummary() {
		return articleSummary;
	}
	public void setArticleSummary(String articleSummary) {
		this.articleSummary = articleSummary;
	}
	public String getTagValue() {
		return tagValue;
	}
	public void setTagValue(String tagValue) {
		this.tagValue = tagValue;
	}
	public String getMapX() {
		return mapX;
	}
	public void setMapX(String mapX) {
		this.mapX = mapX;
	}
	public String getMapY() {
		return mapY;
	}
	public void setMapY(String mapY) {
		this.mapY = mapY;
	}
	public String getMapAddr() {
		return mapAddr;
	}
	public void setMapAddr(String mapAddr) {
		this.mapAddr = mapAddr;
	}
	public String getMapDesc() {
		return mapDesc;
	}
	public void setMapDesc(String mapDesc) {
		this.mapDesc = mapDesc;
	}
	public String getMapProvider() {
		return mapProvider;
	}
	public void setMapProvider(String mapProvider) {
		this.mapProvider = mapProvider;
	}
	public String getMapType() {
		return mapType;
	}
	public void setMapType(String mapType) {
		this.mapType = mapType;
	}
	
	@Override
	protected JSONObject doRequest(HttpServletRequest request,
			HttpServletResponse response, HttpSession session) throws Exception {
		logger.info("--------------------AjaxAddArticleAction--------------------");
		logger.info("articleTitle:"+this.getArticleTitle());
		logger.info("articleContent:"+this.getArticleContent());
		logger.info("tagValue:"+this.getTagValue());
		
		JSONObject jo = new JSONObject();
		List<TaginfoVO> tagInfoVOList = new ArrayList<TaginfoVO>();
		ResultDto<TaginfoVO> resultDtoAddTags = null;
		ResultDto<BaseVO> resultDtoAddArticle = null;
		UserInfoVO userInfoVO = null;
		MapInfoVO mapInfoVO = null;
		ArticleinfoVO articleInfoVO = null;
		
		try {
			UserInfoVO onlineUser = (UserInfoVO)session.getAttribute(SystemConstants.HoneyComb_LoginUser_Session_Name);
			if(onlineUser == null) {
				jo.put("result", SystemConstants.AJAX_FAILED_LOGIN);
			}else {
				//先生成(获取)标签List
				resultDtoAddTags = this.getiTagService().addTags(this.getTagValue());
				if(!resultDtoAddTags.isSuccess()) {
					jo.put("result", SystemConstants.AJAX_FAILED_ALERT);
					return jo;
				}else {
					tagInfoVOList = (List<TaginfoVO>)resultDtoAddTags.getResultList();
				}
				//封装UserInfoVO
				userInfoVO = new UserInfoVO();
				userInfoVO.setUserId(onlineUser.getUserId());
				

				//封装MapInfoVO
				mapInfoVO = new MapInfoVO();
				mapInfoVO.setMapId(HoneyCombTools.getUUIDString());
				mapInfoVO.setMapCreateDttm(HoneyCombTools.convertCurrentDTTMtoInt());
				mapInfoVO.setMapCreateIp(this.getRQ().getRemoteAddr());
				mapInfoVO.setMapProvider(HoneyCombColumnIndicator.MAP_PROVIDER_AMAP);
				mapInfoVO.setMapAddr(this.getMapAddr());
				mapInfoVO.setMapDesc(this.getMapDesc());
				mapInfoVO.setMapX(HoneyCombTools.string2Double(this.getMapX(), 0));
				mapInfoVO.setMapY(HoneyCombTools.string2Double(this.getMapY(), 0));
				mapInfoVO.setMapType(HoneyCombColumnIndicator.MAP_TYPE_OF_ARTICLE);
				
				//封装ArticleInfoVO
				articleInfoVO = new ArticleinfoVO();
				articleInfoVO.setArticleId(HoneyCombTools.getUUIDString());
				articleInfoVO.setArticleTitle(this.getArticleTitle());
				articleInfoVO.setArticleContent(this.getArticleContent());
				articleInfoVO.setArticleSummary(this.getArticleSummary());
				/**
				 * 此处tag字符串只存了输入tag
				 */
				articleInfoVO.setArticleTag(this.getTagValue());
				articleInfoVO.setArticleCreateIp(this.getRQ().getRemoteAddr());
				articleInfoVO.setArticleCreateDttm(HoneyCombTools.convertCurrentDTTMtoInt());
				articleInfoVO.setArticleReplyCnt(SystemConstants.ARTICLE_CREATE_REPLY_CNT);
				articleInfoVO.setArticleVisitCnt(SystemConstants.ARTICLE_CREATE_VISIT_CNT);
				articleInfoVO.setArticleSeq(SystemConstants.ARTICLE_CREATE_SEQ);
				articleInfoVO.setArticleStatus(SystemConstants.ARTICLE_STATUS_NORMAL);
				
				resultDtoAddArticle = this.getiArticleService().doAddArticleInfo(articleInfoVO, userInfoVO, mapInfoVO, tagInfoVOList);
				if(resultDtoAddArticle.isSuccess()) {
					jo.put("result", SystemConstants.AJAX_SUCCESS);
				}else {
					jo.put("result", SystemConstants.AJAX_FAILED_ALERT);
				}
			}
			return jo;
		} catch (Exception e) {
			e.printStackTrace();
			jo.put("result", SystemConstants.AJAX_FAILED_ALERT);
			jo.put("msg", ErrorMsgConstants.ERR_SYSTEM);
			return jo;
		}
	}

}


















